﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Collections;

namespace DAL
{
    public class IBDetailTempSv : UpdateData
    {


        CRMEntities db;
       
        public CustomerEntity LoadCustomerByPhone(string phone)
        {
            db = new CRMEntities();
            if (!string.IsNullOrEmpty(phone.ToString()))
            {
                var query = (from c in db.InBoundDetailTemps
                             where c.CellPhoneNumber.Equals(phone) && !string.IsNullOrEmpty(phone) ||
                             c.OfficePhoneNumber.Equals(phone) && !string.IsNullOrEmpty(phone) ||
                             c.HomePhoneNumber.Equals(phone) && !string.IsNullOrEmpty(phone)
                             join re in db.Regions on c.Province_Code equals re.Region_Code
                             orderby c.ID descending
                           
                             select new CustomerEntity
                             {
                                 //main info                               
                                 FirstName = c.FirstName,
                                 LastName = c.LastName,
                                 Note = c.Note,
                                 EDD = c.EDD,
                                 IDNumber = c.IDNumber,
                                 DateOfBirth = c.DateOfBirth,
                                 HomeNumber = c.HomeNumber,
                                 SubStreet = c.Substreet,
                                 Street = c.Street,
                                 Ward_Code = c.Ward_Code,
                                 District_Code = c.District_Code,
                                 Province_Code = c.Province_Code,
                                 HomePhoneNumber = c.HomePhoneNumber,
                                 CellPhoneNumber = c.CellPhoneNumber,
                                 OfficePhoneNumber = c.OfficePhoneNumber,
                                 CurrentBrandName = c.CurrentBrandName,
                                 PreviousBrandName = c.PreviousBrandName,
                             }).ToList();
                    if (query.Count > 0) { return query[0]; }
                    else    { return null; }
                }
                else
                {
                    return null;
                }
        }


        //public List<IBDetailEntity> LoadAllIBDetail()
        //{
        //    db = new CRMEntities();
        //    var query = (from IB in db.IBDetailTemps
        //                 select new IBDetailEntity
        //                 {
        //                     DateOfComplain = (DateTime)IB.DateOfComplain,
        //                     SourceReciving = IB.SourceReciving,
        //                     NSX = (DateTime)IB.NXS,
        //                     HSD = (DateTime)IB.HSD,
        //                     BrandNameID = IB.BrandNameID,
        //                     ReasonComplain = IB.ReasonComplain,
        //                     Status = IB.Status,
        //                     CRMResponse = IB.CRMResponse,
        //                     SupplyResponse = IB.SupplyResponse,
        //                     FactoryResponse = IB.FactoryResponse,
        //                     ProductExchange = IB.ProductExchange,
        //                     GiftProduct = IB.GiftProduct,
        //                     DatetoFeedBack = (DateTime)IB.DatetoFeedBack
        //                 }).ToList();

        //    return query;
        //}

        public ArrayList LoadIBDetailByPhone(string phone)
        {
            db = new CRMEntities();
            ArrayList array = new ArrayList();
            var query = (from IB in db.InBoundDetailTemps
                         
                         where IB.CellPhoneNumber.Equals(phone) && !string.IsNullOrEmpty(phone)||
                         IB.OfficePhoneNumber.Equals(phone) && !string.IsNullOrEmpty(phone)||
                         IB.HomePhoneNumber.Equals(phone) && !string.IsNullOrEmpty(phone)
                         orderby IB.ID ascending
                         select new IBDetailEntity
                         {
                             DateOfComplain = IB.DateOfComplain,
                             SourceReciving = IB.SourceReciving,
                             NSX = (DateTime)IB.NXS,
                             HSD = (DateTime)IB.HSD,
                             BrandNameID = IB.BrandName_ID,
                             ReasonComplain = IB.ReasonComplain,
                             Status = IB.Status,
                             CRMResponse = IB.CRMResponse,
                             SupplyResponse = IB.SupplyResponse,
                             FactoryResponse = IB.FactoryResponse,
                             ProductExchange = IB.ProductExchange,
                             GiftProduct = IB.GiftProduct,
                             DatetoFeedBack = IB.DatetoFeedBack,
                             CellPhoneNumber = IB.CellPhoneNumber,
                             HomePhoneNumber = IB.HomePhoneNumber,
                             OfficePhoneNumber = IB.OfficePhoneNumber,
                         }).ToList();
            array.Add(query);

            var tmp = (from re in db.Records
                       where re.SourceDestination.Equals(phone)
                           && re.Type == "IN"
                           orderby re.ID ascending
                           select new RecordEntity
                           {                               
                               RecordID = re.ID,
                               RecordLink = re.RecordLink,
                               DateReceived = re.DateReceived,
                               Duration = re.Duration,
                           }).ToList();
            array.Add(tmp);

            return array;
        }


        //public void UpdateCustomer(CustomerEntity cus)
        //{
        //    CRMEntities db = new CRMEntities();


        //    var _cus = db.IBDetailTemps.Where(c => c.CellPhoneNumber.Equals(cus.CellPhoneNumber) || c.OfficePhoneNumber.Equals(cus.OfficePhoneNumber) || c.HomePhoneNumber.Equals(cus.HomePhoneNumber)).SingleOrDefault();

        //        _cus.FirstName = cus.FirstName;
        //        _cus.LastName = cus.LastName;
        //        _cus.Note = cus.Note;
        //        _cus.EDD = cus.EDD;
        //        _cus.IDNumber = cus.IDNumber;
        //        _cus.DateOfBirth = cus.DateOfBirth;
        //        _cus.HomeNumber = cus.HomeNumber;
        //        _cus.Substreet = cus.SubStreet;
        //        _cus.Street = cus.Street;
        //        _cus.Ward_Code = cus.Ward_Code;
        //        _cus.District_Code = cus.District_Code;
        //        _cus.Province_Code = cus.Province_Code;
        //        _cus.HomePhoneNumber = cus.HomePhoneNumber;
        //        _cus.OfficePhoneNumber = cus.OfficePhoneNumber;
        //        _cus.CellPhoneNumber = cus.CellPhoneNumber;
        //        _cus.CurrentBrandName = (int)cus.CurrentBrandName;
        //        _cus.PreviousBrandName = cus.PreviousBrandName;

        //        db.SaveChanges();
            
          
        //}

        public void AddIBDetailTemp(CustomerEntity cus,IBDetailEntity _ib)
        {
             db = new CRMEntities();

             InBoundDetailTemp ib = new InBoundDetailTemp();
             ib.FirstName = cus.FirstName;
             ib.LastName = cus.LastName;
             ib.Note = cus.Note;
             ib.EDD = cus.EDD;
             ib.IDNumber = cus.IDNumber;
             ib.DateOfBirth = cus.DateOfBirth;
             ib.HomeNumber = cus.HomeNumber;
             ib.Substreet = cus.SubStreet;
             ib.Street = cus.Street;
             ib.Ward_Code = cus.Ward_Code;
             ib.District_Code = cus.District_Code;
             ib.Province_Code = cus.Province_Code;
             ib.HomePhoneNumber = cus.HomePhoneNumber;
             ib.OfficePhoneNumber = cus.OfficePhoneNumber;
             ib.CellPhoneNumber = cus.CellPhoneNumber;
             ib.CurrentBrandName = (int)cus.CurrentBrandName;
             ib.PreviousBrandName = cus.PreviousBrandName;


            ib.DateOfComplain = _ib.DateOfComplain;
            ib.SourceReciving = _ib.SourceReciving;
            ib.NXS = _ib.NSX;
            ib.HSD = _ib.HSD;
            ib.BrandName_ID = _ib.BrandNameID;
            ib.ReasonComplain = _ib.ReasonComplain;
            ib.Status = _ib.Status;
            ib.CRMResponse = _ib.CRMResponse;
            ib.SupplyResponse = _ib.SupplyResponse;
            ib.FactoryResponse = _ib.FactoryResponse;
            ib.ProductExchange = _ib.ProductExchange;
            ib.GiftProduct = _ib.GiftProduct;
            ib.DatetoFeedBack = _ib.DatetoFeedBack;

            db.InBoundDetailTemps.Add(ib);
            db.SaveChanges();
            db.Dispose();
        }

        //public void UpdateIBDetail(IBDetailEntity _ib)
        //{
        //    CRMEntities db = new CRMEntities();
        //    var query = (from ib in db.IBDetails
        //                 where ib.ID == _ib.ID
        //                 select ib).SingleOrDefault();
        //    if (query != null)
        //    {
        //        query.DateOfComplain = _ib.DateOfComplain;
        //        query.SourceReciving = _ib.SourceReciving;
        //        query.Customer_ID = _ib.Customer_ID;
        //        query.NXS = _ib.NSX;
        //        query.HSD = _ib.HSD;
        //        query.BrandNameID = _ib.BrandNameID;
        //        query.ReasonComplain = _ib.ReasonComplain;
        //        query.Status = _ib.Status;
        //        query.CRMResponse = _ib.CRMResponse;
        //        query.SupplyResponse = _ib.SupplyResponse;
        //        query.FactoryResponse = _ib.FactoryResponse;
        //        query.ProductExchange = _ib.ProductExchange;
        //        query.GiftProduct = _ib.GiftProduct;
        //        query.DatetoFeedBack = _ib.DatetoFeedBack;

        //    }

        //    db.SaveChanges();
        //    db.Dispose();
        //    return;
        //}
        

      //  [FirstName]
      //,[LastName]
      //,[Note]
      //,[EDD]
      //,[IDNumber]
      //,[DateOfBirth]
      //,[CustomerStatus]
      //,[HomeNumber]
      //,[Substreet]
      //,[Street]
      //,[Ward_Code]
      //,[District_Code]
      //,[Province_Code]
      //,[CellPhoneNumber]
      //,[HomePhoneNumber]
      //,[OfficePhoneNumber]
      //,[CurrentBrandName]
      //,[PreviousBrandName]
      //,[DateOfComplain]
      //,[SourceReciving]
      //,[NXS]
      //,[HSD]
      //,[BrandNameID]
      //,[ReasonComplain]
      //,[Status]
      //,[CRMResponse]
      //,[SupplyResponse]
      //,[FactoryResponse]
      //,[ProductExchange]
      //,[GiftProduct]
      //,[DatetoFeedBack]
    }
}
